package com.apartment.own.controller;

import java.util.List;

import com.apartment.own.request.house.HouseInsertRequest;
import com.apartment.own.request.house.HouseQuery;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.codec.json.AbstractJackson2Decoder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.apartment.common.annotation.Log;
import com.apartment.common.core.controller.BaseController;
import com.apartment.common.core.domain.AjaxResult;
import com.apartment.common.enums.BusinessType;
import com.apartment.own.domain.OwnResHouseResource;
import com.apartment.own.service.IOwnResHouseResourceService;
import com.apartment.common.utils.poi.ExcelUtil;
import com.apartment.common.core.page.TableDataInfo;

/**
 * 房源Controller
 * 
 * @author @Dog_E
 * @date 2020-08-28
 */
@RestController
@ApiOperation("房源信息管理")
@RequestMapping("/own/resource")
public class OwnResHouseResourceController extends BaseController
{
    @Autowired
    private IOwnResHouseResourceService ownResHouseResourceService;

    /**
     * 查询房源列表
     */
    @PreAuthorize("@ss.hasPermi('own:resource:list')")
    @GetMapping("/list")
    public TableDataInfo list(OwnResHouseResource ownResHouseResource)
    {
        startPage();
        List<OwnResHouseResource> list = ownResHouseResourceService.selectOwnResHouseResourceList(ownResHouseResource);
        return getDataTable(list);
    }

    /**
     * 导出房源列表
     */
    @PreAuthorize("@ss.hasPermi('own:resource:export')")
    @Log(title = "房源", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OwnResHouseResource ownResHouseResource)
    {
        List<OwnResHouseResource> list = ownResHouseResourceService.selectOwnResHouseResourceList(ownResHouseResource);
        ExcelUtil<OwnResHouseResource> util = new ExcelUtil<OwnResHouseResource>(OwnResHouseResource.class);
        return util.exportExcel(list, "resource");
    }

    /**
     * 获取房源详细信息
     */
    @PreAuthorize("@ss.hasPermi('own:resource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ownResHouseResourceService.selectOwnResHouseResourceById(id));
    }

    /**
     * 新增房源
     */
    @PreAuthorize("@ss.hasPermi('own:resource:add')")
    @Log(title = "房源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OwnResHouseResource ownResHouseResource)
    {
        return toAjax(ownResHouseResourceService.insertOwnResHouseResource(ownResHouseResource));
    }

    /**
     * 修改房源
     */
    @PreAuthorize("@ss.hasPermi('own:resource:edit')")
    @Log(title = "房源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OwnResHouseResource ownResHouseResource)
    {
        return toAjax(ownResHouseResourceService.updateOwnResHouseResource(ownResHouseResource));
    }

    /**
     * 删除房源
     */
    @PreAuthorize("@ss.hasPermi('own:resource:remove')")
    @Log(title = "房源", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ownResHouseResourceService.deleteOwnResHouseResourceByIds(ids));
    }

    @PostMapping("edit")
    @ApiOperation("编辑房源")
    public AjaxResult editBaseHouseInformation(@RequestBody @Validated HouseInsertRequest request){
        return toAjax(ownResHouseResourceService.editBaseHouseInformation(request));

    }

    @PostMapping("list_house")
    @ApiOperation("查询房源列表")
    public AjaxResult listHouse(@RequestBody @Validated HouseQuery query){
        return AjaxResult.success(ownResHouseResourceService.listHouse(query));
    }

    @GetMapping("house_info")
    @ApiOperation("查询房源信息,和产权信息")
    public AjaxResult houseInfo(String id){
        return AjaxResult.success(ownResHouseResourceService.houseInfo(id));
    }

    /**
     * 查询已有城市
     * @return
     */
    @PostMapping("list_exist_city")
    @ApiOperation("查询城市列表（已有房源城市）")
    public AjaxResult listExistCity(){
        return AjaxResult.success(ownResHouseResourceService.listExistCity());
    }

    /**
     * 查询已有小区
     */
    @PostMapping("list_exist_plot")
    @ApiOperation("查询已有小区")
    public AjaxResult listExistPlot(@RequestBody(required = false) String[] cityId){
        return AjaxResult.success(ownResHouseResourceService.listExistPlot(cityId));
    }

    @PostMapping("list_house_by_house_name")
    @ApiOperation("查询房源信息")
    public AjaxResult listHouseByHouseName(@RequestBody(required = false) HouseQuery request){
        return AjaxResult.success(ownResHouseResourceService.listHouseByHouseName(request));
    }
}
